from functools import wraps

LOG_TYPE = [('issue', '发放'), ('use', '使用'), ('expired', '过期')]


def record_issue_info(func):
    @wraps(func)
    def wrapper(self, *args):
        coupon_id = func(self, *args)

        # 记录发券记录
        coupon_log_data = {
            'root_category': LOG_TYPE[0][0],
            'rp_customer_id': self.rp_customer_id.id,
            'customer_coupon_ids': [(6, 0, coupon_id.ids)],
            'operation_person': self.coupon_issuer_id.id
        }
        # 创建发券记录
        self.env['rp.marketing.action.log'].create([coupon_log_data])

    return wrapper


def record_use_coupon_info(func):
    @wraps(func)
    def wrapper(self, *args):
        func(self, *args)
        if len(self.rp_shipper_order_coupon_line_ids) > 0:
            coupon_template_list = []
            # 封装优惠券模板信息
            for item in self.rp_shipper_order_coupon_line_ids:
                coupon_template_list.append(item.rp_customer_coupon_id.rp_coupon_template_id.id)
            # 记录优惠券使用记录
            log_data = {
                'root_category': LOG_TYPE[1][0],
                'rp_customer_id': self.customer_id.id,
                'customer_coupon_ids': [(6, 0, self.rp_shipper_order_coupon_line_ids.mapped('rp_customer_coupon_id').ids)],
                'ship_order_id': self.id,
                'change_type': 'subtract',
                'funds_change': self.total_actual_price
            }

            # 创建使用优惠券记录
            self.env['rp.marketing.action.log'].create([log_data])

    return wrapper
